package com.tempjob.app.controller;

import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageParam;
import com.anwen.mongo.model.PageResult;
import com.tempjob.app.security.annotation.UserLogIgore;
import com.tempjob.common.businesss.mongo.entity.*;
import com.tempjob.common.businesss.mongo.service.*;
import com.tempjob.common.exception.BusinessException;
import com.tempjob.common.local.LocalAreaService;
import com.tempjob.common.model.AreaVO;
import com.tempjob.common.model.QueryAreaDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hui
 * @Date 2022/4/14 13:54
 **/
@RequestMapping("/sys/area")
@RestController
@Api(tags = "地区管理")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class AreaController {

    @Autowired
    private LocalAreaService localAreaService;

    @Autowired
    private RegionStreetService streetService;

    @Autowired
    private RegionCommunityService communityService;

    @Autowired
    private RegionRoadService roadService;

    @Autowired
    private RegionEstateService estateService;

    @Autowired
    private RegionOthersService othersService;

    @PostMapping("list")
    @ApiOperation(value = "获取省市区列表")
    public Object list() {
        return localAreaService.getAllAreaList();
    }

    @PostMapping("getStreet")
    @ApiOperation(value = "获取街道")
    @UserLogIgore
    public Object getStreet(@RequestBody QueryAreaDTO dto) {
        if (dto.getArea() == null) {
            throw new BusinessException("区编码不能为空");
        }
        LambdaQueryChainWrapper queryWrapper = streetService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq("area_code", dto.getArea());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());
        return streetService.page(queryWrapper,new PageParam(dto.getPage(), dto.getSize()));
    }

    @PostMapping("getStreetCommunity")
    @ApiOperation(value = "获取街道及下面社区")
    @UserLogIgore
    public Object getStreetCommunity(@RequestBody QueryAreaDTO dto) {
        if (dto.getArea() == null) {
            throw new BusinessException("区编码不能为空");
        }
        LambdaQueryChainWrapper queryWrapper = streetService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq("area_code", dto.getArea());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());
        PageResult<RegionStreet> streets = streetService.page(queryWrapper,new PageParam(dto.getPage(), dto.getSize()));
        if (!streets.getContentData().isEmpty()) {
            List<Long> streetCodes = streets.getContentData().stream().map(RegionStreet::getCode).collect(Collectors.toList());
            List<RegionCommunity> communitys = communityService.lambdaQuery().in(RegionCommunity::getStreetCode, streetCodes).list();
            streets.getContentData().forEach(i -> {
                i.setChildren(communitys.stream().filter(c -> c.getStreetCode().equals(i.getCode())).collect(Collectors.toList()));
            });
        }
        return streets;
    }

    @PostMapping("getOther")
    @ApiOperation(value = "获取街道其他设置")
    @UserLogIgore
    public Object getOther(@RequestBody QueryAreaDTO dto) {
        LambdaQueryChainWrapper queryWrapper = othersService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq(dto.getArea() != null, "areaCode", dto.getArea());
        queryWrapper.eq(dto.getStreet() != null, "streetCode", dto.getStreet());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());
        PageResult<RegionOthers> streets = othersService.page(queryWrapper,new PageParam(dto.getPage(), dto.getSize()));
        streets.getContentData().forEach(i -> {
            if (i.getStreetcode() != null) {
                AreaVO street = localAreaService.getStreetByCode(i.getStreetcode().longValue());
                if (street != null) {
                    i.setStreetName(street.getName());
                }
            }
        });
        return streets;
    }


    @PostMapping("getRoad")
    @ApiOperation(value = "获取道路")
    @UserLogIgore
    public Object getRoad(@RequestBody QueryAreaDTO dto) {
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        if (dto.getArea() == null && dto.getStreet() == null) {
            throw new BusinessException("区编码和街道编码不能同时为空！");
        }
        LambdaQueryChainWrapper queryWrapper = roadService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq(dto.getArea() != null, "area_code", dto.getArea());
        queryWrapper.eq(dto.getStreet() != null, "street_code", dto.getStreet());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());
        PageResult<RegionRoad> roads = roadService.page(queryWrapper,page);
//        return localAreaService.getRoad(new QueryAreaDTO(null,null,areaCode,streetCode,null));
        roads.getContentData().forEach(i -> {
            AreaVO street = localAreaService.getStreetByCode(i.getStreetCode());
            if (street != null) {
                i.setStreetName(street.getName());
            }
        });
        return roads;
    }

    @PostMapping("getCommunity")
    @ApiOperation(value = "获取社区")
    @UserLogIgore
    public Object getCommunity(@RequestBody QueryAreaDTO dto) {
        if (dto.getArea() == null && dto.getStreet() == null) {
            throw new BusinessException("区编码和街道编码不能同时为空！");
        }
        LambdaQueryChainWrapper queryWrapper = communityService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq(dto.getArea() != null, "area_code", dto.getArea());
        queryWrapper.eq(dto.getStreet() != null, "street_code", dto.getStreet());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());

        PageResult<RegionCommunity> community = communityService.page(queryWrapper,new PageParam(dto.getPage(), dto.getSize()));

        community.getContentData().forEach(i -> {
            if (i.getStreetCode() != null) {
                AreaVO street = localAreaService.getStreetByCode(i.getStreetCode());
                if (street != null) {
                    i.setStreetName(street.getName());
                }
            }
        });
        return community;
//        return localAreaService.getCommunity(new QueryAreaDTO(null,null,areaCode,streetCode,null));
    }

    @PostMapping("getEstate")
    @ApiOperation(value = "获取小区")
    @UserLogIgore
    public Object getEstate(@RequestBody QueryAreaDTO dto) {
        if (dto.getArea() == null && dto.getStreet() == null) {
            throw new BusinessException("区编码和街道编码不能同时为空！");
        }
        LambdaQueryChainWrapper queryWrapper = estateService.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByAsc("code");
        }
        queryWrapper.eq(dto.getArea() != null, "area_code", dto.getArea());
        queryWrapper.eq(dto.getStreet() != null, "street_code", dto.getStreet());
        queryWrapper.eq(dto.getCommunity() != null, "community_code", dto.getCommunity());
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), "name", dto.getName());
        PageResult<RegionEstate> estate = estateService.page(queryWrapper,new PageParam(dto.getPage(), dto.getSize()));
        estate.getContentData().forEach(i -> {
            AreaVO street = localAreaService.getStreetByCode(i.getStreetCode());
            AreaVO community = localAreaService.getCommunitytByCode(i.getCommunityCode());
            if (street != null) {
                i.setStreetName(street.getName());
            }
            if (community != null) {
                i.setCommunityName(community.getName());
            }
        });
        return estate;
    }


}
